<?php
namespace Util;
use think\Db;
use think\Session;

class Sysdb{

	// 指定表名
	public function table($table){
        $this->where = array();
        $this->whereOr = array();
		$this->field = '*';
		$this->order = '';
		$this->limit = 0;
        $this->table = $table;
        $this->distinct = false;
        $this->leftJoint = '';
        $this->leftJoins = '';
		return $this;
	}

	// 指定查询字段
	public function field($field = '*'){
		$this->field = $field;
		return $this;
	}

	// 加载数量
	public function limit($limit){
		$this->limit = $limit;
		return $this;
	}

	// 排序
	public function order($order){
		$this->order = $order;
		return $this;
	}

	// 指定查询条件
	public function where($where = array()){
		$this->where = $where;
		return $this;
	}

    // 去重
    public function distinct($distinct = false){
        $this->distinct = $distinct;
        return $this;
    }

    //
    public function leftJoin($t,$s){
        $this->leftJoint = $t;
        $this->leftJoins = $s;
        return $this;
    }

    // 指定查询条件
    public function whereOr($whereOr = array()){
        $query = Db::name($this->table)->field($this->field)->whereOr($whereOr);
        $lists = $query->select();
        //echo Db::name('live_course')->getLastSql();
        return $lists;
    }

	// 自增：index自增哪个字段，value一次自增多大
	public function setInc($index,$value = 1){
		$res = Db::name($this->table)->where($this->where)->setInc($index,$value);
		return $res;
	}

	// 自减
	public function setDec($index,$value = 1){
		$res = Db::name($this->table)->where($this->where)->setDec($index,$value);
		return $res;
	}

	// 获取总数
	public function count(){
		return Db::name($this->table)->where($this->where)->count();
	}

	// 返回一条记录
	public function item(){
		$item = Db::name($this->table);
        $this->field && $item = $item->field($this->field);
        $this->where && $item = $item->where($this->where);
        $this->leftJoint && $item = $item->leftJoin($this->leftJoint,$this->leftJoins);
        $item = $item->find();
        $this->writeLog('查询数据：'.json_encode($item,JSON_UNESCAPED_UNICODE),'sql');
        return $item ? $item : false;
	}

	// 返回list
	public function lists(){
		$query = Db::name($this->table)->field($this->field)->distinct($this->distinct)->where($this->where);
        $this->limit && $query = $query->limit($this->limit);
        $this->order && $query = $query->order($this->order);
        $this->leftJoint && $query = $query->leftJoin($this->leftJoint,$this->leftJoins);
        $lists = $query->select();
        $this->writeLog('批量查询：'.$this->table.'-'.json_encode($this->where,JSON_UNESCAPED_UNICODE)
            .'-'.json_encode($this->leftJoint,JSON_UNESCAPED_UNICODE)
            .'-'.json_encode($this->leftJoins,JSON_UNESCAPED_UNICODE),'sql');
        return $lists ? $lists : [];
	}

	// 自定义索引列表
	public function cates($index){
		$query = Db::name($this->table)->field($this->field)->where($this->where);
		$this->limit && $query = $query->limit($this->limit);
		$this->order && $query = $query->order($this->order);
		$lists = $query->select();

		if(!$lists){
			return [];
		}
		$results = [];
		foreach ($lists as $key => $value) {
			$results[$value[$index]] = $value;
		}
		return $results;
	}

	// 分页
	public function pages($pageSize = 10){
		$total = Db::name($this->table)->where($this->where)->count();
		$query = Db::name($this->table)->field($this->field)->where($this->where);
		$this->order && $query = $query->order($this->order);
        $this->leftJoint && $query = $query->leftJoin($this->leftJoint,$this->leftJoins);
        $data = $query->paginate($pageSize,$total);
		return array('total'=>$total,'lists'=>$data->items(),'pages'=>$data->render());
	}

	// 添加
	public function insert($data){
		$res = Db::name($this->table)->insertGetId($data);
		$this->writeLog('添加数据：'.json_encode($data,JSON_UNESCAPED_UNICODE),'sql');
		return $res;
	}
	// 批量添加
	public function insertAll($data){
		$res = Db::name($this->table)->insertAll($data);
        $this->writeLog('批量添加数据：'.json_encode($data,JSON_UNESCAPED_UNICODE),'sql');
        return $res;	// 返回添加成功的条数
	}

	// 修改
	public function update($data){
		$res = Db::name($this->table)->where($this->where)->update($data);
        $this->writeLog('修改数据：'.json_encode($data,JSON_UNESCAPED_UNICODE),'sql');
        return $res;
	}



	// 删除
	public function delete(){
        $this->writeLog('删除数据','sql');
        $res = Db::name($this->table)->where($this->where)->delete();
		return $res;
	}

	//写日志
    public function writeLog($content,$filename='Create')
    {
        $logFileDir = DOCUMENT_ROOT.'/Log';
        if (!is_dir($logFileDir)){
            mkdir($logFileDir,0777,true);
        }
        $path = $logFileDir.'/'.$filename;
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }
        $file = $path.'/'.$filename.'_'.date('Y_m_d').'.txt';
        file_put_contents($file,date('YmdHis',time()).PHP_EOL.Db::table($this->table)->getLastSql().PHP_EOL.'用户'.Session('admin.truename').'表'.$this->table.'条件'.json_encode($this->where,JSON_UNESCAPED_UNICODE).$content.PHP_EOL,FILE_APPEND);  // 最简单的快速的以追加的方式写入写入方法，
    }
}